package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.KingBaseReceiptPayPurposeMapper;
import com.ly.heart.service.IKingBaseReceiptPayPurposeService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseReceiptPayPurposeEntity;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 金蝶-财务模块基础数据-收付款用途Service业务层处理
 *
 * @author wutao
 * @date 2024-06-20
 */
@Service
public class KingBaseReceiptPayPurposeServiceImpl extends ServiceImpl<KingBaseReceiptPayPurposeMapper, KingBaseReceiptPayPurposeEntity> implements IKingBaseReceiptPayPurposeService {

    /**
     * 查询金蝶-财务模块基础数据-收付款用途
     *
     * @param id 金蝶-财务模块基础数据-收付款用途ID
     * @return 金蝶-财务模块基础数据-收付款用途
     */
    @Override
    public KingBaseReceiptPayPurposeEntity selectKingBaseReceiptPayPurposeEntityById ( String id ) {
        return this.getById ( id );
    }

    /**
     * 查询金蝶-财务模块基础数据-收付款用途列表
     *
     * @param kingBaseReceiptPayPurposeEntity 金蝶-财务模块基础数据-收付款用途
     * @return 金蝶-财务模块基础数据-收付款用途
     */
    @Override
    public AjaxResult selectKingBaseReceiptPayPurposeEntityList ( KingBaseReceiptPayPurposeEntity kingBaseReceiptPayPurposeEntity ) {
        LambdaQueryWrapper<KingBaseReceiptPayPurposeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<> ();
        if ( StringUtils.isNotBlank ( kingBaseReceiptPayPurposeEntity.getReceiptPayType () ) ) {
            lambdaQueryWrapper.eq ( KingBaseReceiptPayPurposeEntity::getReceiptPayType, kingBaseReceiptPayPurposeEntity.getReceiptPayType () );
        }
        lambdaQueryWrapper.eq ( KingBaseReceiptPayPurposeEntity :: getIsUsed, ConstUtil.IS_USED_YES );
        IPage page = this.page ( new Page<> ( kingBaseReceiptPayPurposeEntity.getPage (), kingBaseReceiptPayPurposeEntity.getLimit () ), lambdaQueryWrapper );
        List<KingBaseReceiptPayPurposeEntity> records = page.getRecords ();
        records.forEach ( pay -> {
            String bizType = "1".equals ( pay.getBizTyep () ) ? "销售业务" : ( "2".equals ( pay.getBizTyep () ) ? "采购业务" : ( "3".equals ( pay.getBizTyep () ) ? "其他业务" : "工资发放" ) );
            pay.setBizTyep ( bizType );
        } );
        page.setRecords ( records );
        return AjaxResult.success ( page );
    }

    /**
     * 新增金蝶-财务模块基础数据-收付款用途
     *
     * @param kingBaseReceiptPayPurposeEntity 金蝶-财务模块基础数据-收付款用途
     * @return 结果
     */
    @Override
    public AjaxResult insertKingBaseReceiptPayPurposeEntity ( KingBaseReceiptPayPurposeEntity kingBaseReceiptPayPurposeEntity ) {
        kingBaseReceiptPayPurposeEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        kingBaseReceiptPayPurposeEntity.setBizCode ( "SFKYT".concat ( String.format ( "%08d", count ) ) );
        kingBaseReceiptPayPurposeEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseReceiptPayPurposeEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( kingBaseReceiptPayPurposeEntity );
        if ( ! save ) {
            throw new CustomException ( "新增收付款用途失败！" );
        }
        return AjaxResult.success ( "新增收付款用途成功" );
    }


    /**
     * 修改金蝶-财务模块基础数据-收付款用途
     *
     * @param kingBaseReceiptPayPurposeEntity 金蝶-财务模块基础数据-收付款用途
     * @return 结果
     */
    @Override
    public AjaxResult updateKingBaseReceiptPayPurposeEntity ( KingBaseReceiptPayPurposeEntity kingBaseReceiptPayPurposeEntity ) {
        kingBaseReceiptPayPurposeEntity.setUpdateTime ( DateUtils.getNowDate () );
        kingBaseReceiptPayPurposeEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        boolean b = this.updateById ( kingBaseReceiptPayPurposeEntity );
        if ( ! b ) {
            throw new CustomException ( "修改收付款用途失败！" );
        }
        return AjaxResult.success ( "修改收付款用途成功" );
    }

    /**
     * 批量删除金蝶-财务模块基础数据-收付款用途
     *
     * @param ids 需要删除的金蝶-财务模块基础数据-收付款用途ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseReceiptPayPurposeEntityByIds ( String[] ids ) {
        List<KingBaseReceiptPayPurposeEntity> list = this.list ( new LambdaQueryWrapper<KingBaseReceiptPayPurposeEntity> ().in ( KingBaseReceiptPayPurposeEntity :: getId, ids ) );
        List<String> idList = list.stream ().filter ( pay -> pay.getIsDel () == 1 ).map ( KingBaseReceiptPayPurposeEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( pay -> {
            if ( pay.getIsDel () == 2 ) {
                stringBuilder.append ( pay.getTitle () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseReceiptPayPurposeEntity> ().
                set ( KingBaseReceiptPayPurposeEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBaseReceiptPayPurposeEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseReceiptPayPurposeEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingBaseReceiptPayPurposeEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除收付款用途失败！" );
        }
        String str = "删除收付款用途成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-财务模块基础数据-收付款用途信息
     *
     * @param id 金蝶-财务模块基础数据-收付款用途ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled ( String id ) {
        KingBaseReceiptPayPurposeEntity payPurposeEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseReceiptPayPurposeEntity> ().
                set ( KingBaseReceiptPayPurposeEntity :: getIsDisabled, payPurposeEntity.getIsDisabled () == 1 ? 2 : 2 ).
                set ( KingBaseReceiptPayPurposeEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseReceiptPayPurposeEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingBaseReceiptPayPurposeEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( payPurposeEntity.getIsDisabled () == 1 ? "禁用收付款用途失败！" : "解禁收付款用途失败！" );
        }
        return AjaxResult.success ( payPurposeEntity.getIsDisabled () == 1 ? "禁用收付款用途成功" : "解禁收付款用途成功" );
    }

}
